<template>
	<view class="wrap">
		<view class="tabs">
			<view :class="{tab: true, selected: tabIndex === index}" v-for="(item, index) in tabs" @click="changeTab($event, index)">{{item}}</view>
		</view>
		<view class="list">
			<view class="item" v-for="(item, index) in list">
				<view class="title">订单号：{{item.orderNumber}}<text>{{item.createTime}}</text></view>
				<view class="main" v-for="(goods, idx) in item.goodsList">
					<image :src="goods.pic" mode="aspectFill"></image>
					<view class="info">
						<view class="name ellipsis-2">{{goods.goodsName}}</view>
						<!-- <view class="points">积分: 3000</view> -->
						<view class="price">{{goods.price}}<text class="days">/天</text> <text>¥{{goods.deposit}}押金</text></view>
					</view>
				</view>
				<view class="address">
					<view class="addr-detail">地址: 苏州新区石斛东路1000</view>
					<view class="phone"><text>15888888888</text>张先生</view>
				</view>
				<view class="btn-group">
					<button v-if="item.status === 2" @click="takeGoods($event, item, index)">提货</button>
					<button v-if="item.status === 4" @click="giveBack($event, item, index)">确认归还</button>
					<text>{{['待支付', '待提货', '待归还', '待确认', '已完成'][item.status - 1]}}</text>
				</view>
			</view>
		</view>
		<view class="no-data">{{payload.hasmore ? '数据加载中...' : '暂无更多数据'}}</view>
	</view>
</template>

<script>
	import {
		utils,
		numeral
	} from '../../utils/index.js'
	import {
		Order
	} from '../../apis/index.js'

	export default {
		components: {},
		data() {
			return {
				list: [],
				tabs: ['已支付', '配送中', '待归还', '已完成'],
				tabIndex: 0,
				payload: {
					current: 1,
					hasmore: true,
					pageSize: 10
				},
				isLoading: false
			}
		},
		methods: {
			/**
			 * 确定归还
			 * @param {Object} $event
			 * @param {Object} item
			 * @param {Object} index
			 */
			giveBack($event, item, index) {
				var shopInfo = uni.getStorageSync('shopInfo')
				uni.showModal({
					content: '确定已归还？',
					success: res => {
						if (res.confirm) {
							Order.updateOrder({
								data: {
									form: {
										orderNumber: item.orderNumber,
										status: 5,
										userId: this.options.id,
										shopId: shopInfo.id
									}
								}
							}).then(res => {
								console.log('归还成功')
								console.log(res.data)
								if (res.data.code === 200) {
									uni.showToast({
										title: '归还成功'
									})
									this.list[index].status = 5
								} else {
									uni.showToast({
										title: '操作失败',
										icon: 'none'
									});
								}
							})
						}
					}
				})
			},
			takeGoods($event, item, index) {
				var shopInfo = uni.getStorageSync('shopInfo')
				uni.showModal({
					content: '确定已提货？',
					success: res => {
						if (res.confirm) {
							Order.updateOrder({
								data: {
									form: {
										orderNumber: item.orderNumber,
										status: 3,
										shopId: shopInfo.id,
										userId: item.userId
									}
								}
							}).then(res => {
								console.log('提货成功')
								console.log(res.data)
								if (res.data.code === 200) {
									uni.showToast({
										title: '提货成功'
									})
									this.list[index].status = 3
								} else {
									uni.showToast({
										title: '操作失败',
										icon: 'none'
									});
								}
							})
						}
					}
				})
			}
		},
		onLoad(options) {
			this.options = options

			getList.call(this)
		},
		onPullDownRefresh() {
			this.payload.hasmore = true
			this.payload.current = 1
			getList.call(this)
		},
		onReachBottom() {
			if (!this.payload.hasmore) return
			this.payload.current += 1
			getList.call(this)
		}
	}

	function getList() {
		if (this.isLoading) return
		this.isLoading = true
		uni.showLoading({
			title: '加载中...'
		})
		var shopInfo = uni.getStorageSync('shopInfo')

		Order.getOrdersByShop({
			data: {
				query: {
					id: shopInfo.id
				},
				pagination: {
					current: this.payload.current,
					pageSize: this.payload.pageSize
				}
			}
		}).then(res => {
			if (res.data.code === 200) {
				this.payload.hasmore = res.data.body.length === this.payload.pageSize
				res.data.body.map(item => {
					item.goodsList = JSON.parse(item.goodsList)
					item.goodsList.length && item.goodsList.map(goods => {
						goods.pic = goods.pics.split(',')[0]
						goods.price = numeral(goods.price || 0).divide(100).format('0.00')
						goods.deposit = numeral(goods.deposit || 0).divide(100).format('0.00')
					})
				})
				this.list = this.payload.current === 1 ? res.data.body : this.list.concat(res.data.body)
			} else {
				throw new Error("获取订单信息失败")
			}
		}, err => {
			throw new Error(err)
		}).finally(() => {
			this.isLoading = false
			uni.hideLoading()
			uni.stopPullDownRefresh()
		})
	}
</script>

<style lang="scss">
	page {
		background-color: #EFEFEF;
	}

	.list {
		box-sizing: border-box;

		.item {
			box-sizing: border-box;
			margin-top: 10rpx;
			background-color: #FFF;
			padding: 0 20rpx;

			.title {
				font-size: 20rpx;
				color: #666;
				height: 74rpx;
				line-height: 74rpx;
				border-bottom: #EFEFEF 1rpx solid;
				display: flex;
				justify-content: space-between;
			}

			.main {
				display: flex;
				position: relative;
				box-sizing: border-box;
				padding: 10rpx 0rpx;

				image {
					width: 140rpx;
					height: 140rpx;
					border-radius: 10rpx;
					background-color: #D8D8D8;
					flex-shrink: 0;
				}

				.info {
					flex: 1;
					box-sizing: border-box;
					padding-left: 20rpx;

					.name {
						color: #333333;
						font-size: 28rpx;
						height: 80rpx;
						line-height: 40rpx;
					}

					.points {
						color: #666;
						font-size: 24rpx;
					}

					.price {
						color: #FF4A4A;
						font-size: 24rpx;
						font-weight: bold;

						text {
							font-weight: normal;
						}

						&:before {
							content: "¥";
							font-size: 20rpx;
							font-weight: normal;
						}

						.days {
							color: #999;
							padding: 0 10rpx;
						}
					}
				}
			}
			.address {
				box-sizing: border-box;
				padding: 20rpx 0;
				position: relative;
				color: #333333;
				font-size: 24rpx;
				.addr-detail {
					overflow: hidden;
					text-overflow:ellipsis;
					white-space: nowrap;
					line-height: 1.5;
				}
				.phone {
					display: flex;
					text {
						flex: 1;
					}
				}
				&::before {
					content: " ";
					display: block;
					position: absolute;
					left: 0;
					right: 0;
					top: 0;
					height: 0;
					border-top: #EFEFEF 2rpx solid;
				}
				
			}
			.btn-group {
				box-sizing: border-box;
				height: 100rpx;
				display: flex;
				align-items: center;
				flex-direction: row-reverse;
				border-top: #EFEFEF 1rpx solid;

				button {
					width: 166rpx;
					height: 60rpx;
					font-size: 24rpx;
					border: #FF4A4A 2rpx solid;
					border-radius: 30rpx;
					line-height: 60rpx;
					color: #FF4A4A;
					background-color: #FFF;
					margin: unset;
				}

				text {
					flex: 1;
					text-align: left;
					font-size: 24rpx;
					color: #999;
				}
			}
		}
	}
	.tabs {
		height: 90rpx;
		display: flex;
		align-items: center;
		box-sizing: border-box;
		background-color: #FFF;
		overflow-x: scroll;
		padding: 0 20rpx;
	
		.tab {
			color: #333;
			font-size: 28rpx;
			flex-shrink: 0;
			line-height: 90rpx;
			padding: 0 15rpx;
	
			&.selected {
				color: #FF4A4A;
				border-bottom: #FF4A4A 2rpx solid;
			}
		}
	}

	.no-data {
		color: #999999;
		height: 50rpx;
		line-height: 50rpx;
		text-align: center;
		font-size: 28rpx;
		padding: 30rpx;
	}
</style>
